<template>
  <div class="login">
    <div class="login-content">
      <div style="position: absolute; top: -60px">
        <img
          src="@/assets/logo/logo.png"
          alt=""
          style="height: 40px; vertical-align: middle; margin-right: 10px"
        />
        <span style="font-size: 26px; color: #fff; letter-spacing: 1px"
          >这是vue模板</span
        >
        <!--        <img src="@/assets/image/title-logo2.png" alt="" style="height:45px;">-->
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <h2 class="title">用户登录</h2>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入用户名"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <!--            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
          </el-input>
          <div class="login-code" @click="getCode" v-html="codeUrl">
            <!--            <el-image-->
            <!--              :src="codeUrl"-->
            <!--              style="width: 120px; height: 45px"-->
            <!--              :fit="fit"></el-image>-->
          </div>
        </el-form-item>
        <el-form-item style="text-align: left; margin-bottom: 20px">
          <el-checkbox v-model="loginForm.rememberMe"
            ><span style="color: #fff">记住密码</span></el-checkbox
          >
        </el-form-item>

        <el-form-item style="width: 100%">
          <Captcha
            id="Captcha"
            scene="Login"
            type="Geetest"
            :parm="captchaOption"
            @callback="captchaNotice"
            url="http://47.97.34.77:3000/api/captcha/captcha"
          >
            <el-button
              :loading="loading"
              style="width: 100%"
              size="medium"
              type="primary"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </Captcha>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span style="color: black"
        >Copyright © 2020 X-powerise All Rights Reserved.</span
      >
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from "vuex";

import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import test from "../store/modules/test";

export default {
  name: "Login",
  data() {
    return {
      captchaOption: {
        // 各平台的参数，具体请参阅个平台文档
        // 以下为腾讯验证码的参数
        // appid: '',
        // 以下为极验验证码的参数
        product: "bind",
      },
      cookiePassword: "",
      loginForm: {
        email: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        code: [
          { required: true, trigger: "change", message: "验证码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  computed: {
    ...mapGetters(["codeUrl"]),
    ...mapState({
      value: (state) => state.test.value,
    }),
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    // console.log()
    this.getCode();
    this.getCookie();
  },
  methods: {
    // 回调监听
    // status: 1成功,2验证中,0失败
    // res: 三方返回的原始数据
    captchaNotice(status, res) {
      console.log(status);
      console.log(res);
    },
    getCode() {
      this.$store.dispatch("test/getCode");
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("user/Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url("../assets/image/login-bg.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.title {
  /*margin: 0px auto 30px auto;*/
  margin-bottom: 40px;
  text-align: center;
  color: #fff;
  font-size: 30px;
  letter-spacing: 5px;
  font-weight: normal;
}

.login-content {
  background: url("../assets/image/login-box.png") no-repeat;
  width: 500px;
  height: 502px;
  position: absolute;
  left: calc(50% - 250px);
  top: calc(50% - 230px);
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 20px 50px;
}

.login-form {
  width: 400px;
  text-align: center;
  height: calc(100% 90px);

  .el-input {
    height: 45px;

    input {
      height: 45px;
    }

    .el-input__inner {
      height: 45px;
      color: #fff;
      background: rgba(255, 255, 255, 0) !important;
    }
  }

  .input-icon {
    height: 45px;
    width: 16px;
    margin-left: 2px;
  }

  .el-form-item {
    height: 45px;
    margin-bottom: 30px;

    .el-form-item__content {
      height: 100%;
    }

    .el-button {
      font-size: 20px;
      letter-spacing: 15px;
      height: 100%;
    }
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 45px;
  float: right;
  cursor: pointer;
  vertical-align: middle;

  svg {
    width: 100%;
    height: 100%;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
